import dayjs from 'dayjs'
import Models from './dbs'

const api = {
    Login(req: any, res: any, next: any) {
        let account = req.body.account
        let password = req.body.password
        Models.accountInfo.findOne({ 'account': account, 'password': password }, function (err: any, data: object) {
            if (!err && data) {
                res.json({
                    code: 200,
                    msg: '登录成功',
                    result: data
                })
            } else {
                res.json({
                    code: 400,
                    msg: '登录失败'
                })
            }
        })
    },
    AccountAdd(req: any, res: any, next: any) {
        let acc = new Models.accountInfo({
            account: req.body.account,
            password: req.body.password,
            name: req.body.name,
            nickname: req.body.nickname,
            sex: req.body.sex,
            birthday: req.body.birthday,
            power: req.body.power,
            remark: req.body.remark,
            creatTime: dayjs().format('YYYY/MM/DD HH:mm:ss')
        })

        acc.save(function (err: any) {
            if (!err) {
                res.json({
                    code: 200,
                    msg: '操作成功'
                })
            } else {
                res.json({
                    code: 400,
                    msg: '操作失败'
                })
            }
        })
    },
    AccountList(req: any, res: any, next: any) {
        // 要跳过的数量
        let offset = (req.body.pageNo - 1) * req.body.pageSize
        // 要查询的数量
        let limit = req.body.pageSize
        let keyword = new RegExp(req.body.keyword, 'i')
        Models.accountInfo.count({
            $or: [{ account: { $regex: keyword } }, { name: { $regex: keyword } }, { nickname: { $regex: keyword } }]
        }, function (error: any, count: any) {
            if (!error) {
                // 查询到所有数据，根据creatTime顺序排序(1是顺序，-1是倒序)，跳过不需要查询的部分，查询需要查询的数量
                Models.accountInfo.find({ $or: [{ account: { $regex: keyword } }, { name: { $regex: keyword } }, { nickname: { $regex: keyword } }] }).sort({ "creatTime": -1 }).skip(offset).limit(limit).find({}, function (err: any, data: any) {
                    if (!err) {
                        data.forEach((element: any) => {
                            if (element.sex === 'male') {
                                element.sex = '男'
                            } else {
                                element.sex = '女'
                            }
                            if (element.power === 'admin') {
                                element.power = '系统管理员'
                            } else {
                                element.power = '普通用户'
                            }
                        });
                        res.json({
                            code: 200,
                            msg: '查询成功',
                            result: {
                                list: data,
                                total: count
                            }
                        })
                    } else {
                        res.json({
                            code: 400,
                            msg: '查询出错'
                        })
                    }
                })
            } else {
                res.json({
                    code: 400,
                    msg: '查询出错'
                })
            }
        })
    },
    AccountInfo(req: any, res: any, next: any) {
        let id = req.query.id
        Models.accountInfo.findOne({ "_id": id }, function (err: any, data: object) {
            if (!err) {
                res.json({
                    code: 200,
                    msg: '操作成功',
                    result: data
                })
            } else {
                res.json({
                    code: 400,
                    msg: '操作失败'
                })
            }
        })
    },
    AccountUpdate(req: any, res: any, next: any) {
        let id = req.body.id
        let newAcc = {
            account: req.body.account,
            password: req.body.password,
            name: req.body.name,
            nickname: req.body.nickname,
            sex: req.body.sex,
            birthday: req.body.birthday,
            power: req.body.power,
            remark: req.body.remark
        }
        Models.accountInfo.updateOne({ "_id": id }, newAcc, function (err: any, data: object) {
            if (!err) {
                res.json({
                    code: 200,
                    msg: '操作成功'
                })
            } else {
                res.json({
                    code: 400,
                    msg: '操作失败'
                })
            }
        })
    },
    AccountDelete(req: any, res: any, next: any) {
        let id = req.body.id
        Models.accountInfo.deleteOne({ "_id": id }, function (err: any, data: object) {
            if (!err) {
                res.json({
                    code: 200,
                    msg: '操作成功'
                })
            } else {
                res.json({
                    code: 400,
                    msg: '操作失败'
                })
            }
        })
    },
    AccountDeleteMultiple(req: any, res: any, next: any) {
        let ids = req.body.ids
        Models.accountInfo.deleteMany({ "_id": { $in: ids } }, function (err: any, data: object) {
            if (!err) {
                res.json({
                    code: 200,
                    msg: '操作成功'
                })
            } else {
                res.json({
                    code: 400,
                    msg: '操作失败'
                })
            }
        })
    }
}

export default api